package com.letoken.platform.repo.performance;


import com.letoken.platform.pub.enums.TeamPerformanceTypeEnum;
import com.letoken.platform.repo.app.dao.AppUserTeamMapper;
import com.letoken.platform.repo.app.dao.TeamPerformanceMapper;
import com.letoken.platform.repo.app.po.AppUserTeam;
import com.letoken.platform.repo.app.po.TeamPerformance;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;

@Service
@Log4j2
public class TeamPerformanceService {

    @Resource
    private TeamPerformanceMapper teamPerformanceMapper;

    @Resource
    private AppUserTeamMapper appUserTeamMapper;


    public void add(TeamPerformanceTypeEnum type, Integer userId, BigDecimal performance){

        Integer recentLeader;
        AppUserTeam appUserTeam = appUserTeamMapper.selectByUserId(userId);
        if(appUserTeam.getLevel() == 2){
            recentLeader = userId;
        } else {
            recentLeader = teamPerformanceMapper.selectRecentLeader(userId);
        }
        if(null == recentLeader || 0 == recentLeader){
            return;
        }

        TeamPerformance teamPerformance = new TeamPerformance();
        teamPerformance.setUserId(userId);
        teamPerformance.setPerformance(performance);
        teamPerformance.setRecentLeader(recentLeader);
        teamPerformance.setType(type.value);
        teamPerformance.setCreatedAt(LocalDateTime.now());
        teamPerformanceMapper.insert(teamPerformance);
    }


}
